﻿CREATE PROCEDURE [dbo].[yaf_forum_updatestats](
                @ForumID INT)
AS
    BEGIN
        UPDATE yaf_Forum
        SET    NumPosts = (SELECT COUNT(1)
                           FROM   yaf_Message x,
                                  yaf_Topic y
                           WHERE  y.TopicID = x.TopicID
                           AND y.ForumID = yaf_Forum.ForumID
                           AND (x.Flags & 24) = 16),
               NumTopics = (SELECT COUNT(DISTINCT x.TopicID)
                            FROM   yaf_Topic x,
                                   yaf_Message y
                            WHERE  x.ForumID = yaf_Forum.ForumID
                            AND y.TopicID = x.TopicID
                            AND (y.Flags & 24) = 16)
        WHERE  ForumID = @ForumID
    END


